﻿using LayeredSkin.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using Top_Meeting.UI;
using LayeredSkin.DirectUI;
using System.Threading.Tasks;
using LayeredSkin.Forms;
using System.Diagnostics;

using TopVisionVideoStream;
using System.Runtime.InteropServices;
using System.IO;

namespace Top_Meeting.Window
{
    public partial class Frm_Main : Frm_Base
    {
        public static Frm_Main instance = null;

        #region 字段

        private bool mainMenu = false;
        private bool isMenuAnimation = false;

        private int _conferenceMode = 0;

        private Size size1 = new Size(1, 1);

        private LayeredPanel _selectedItem;

        #region Video Conference
        //Display
        private bool isStatusON = true;

        //Conference Control
        private List<LayeredCheckButton> ccCBList = new List<LayeredCheckButton>();
        private List<LayeredCheckButton> ccMarkCubeCBList = new List<LayeredCheckButton>();
        private List<LayeredCheckButton> ccMarkArrCBList = new List<LayeredCheckButton>();
        private List<LayeredCheckButton> ccMarkSolidCBList = new List<LayeredCheckButton>();

        #endregion


        #endregion


        #region PROPERTY

        private LayeredPanel selectedItem
        {
            get
            {
                return _selectedItem;
            }
            set
            {
                if (_selectedItem != value & _selectedItem != null)
                {
                    var _tmp = _selectedItem;
                    _selectedItem = null;
                    if (_tmp != null)
                        SetMenuItem(_tmp, null, _tmp.Name + "_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent);
                    SetMenuItem(value, null, value.Name + "_h", TMColor.Color_39f3ff, TMColor.Color_252628);
                    _selectedItem = value;
                }
                else
                {
                    SetMenuItem(value, null, value.Name + "_h", TMColor.Color_39f3ff, TMColor.Color_252628);
                    _selectedItem = value;
                }
            }
        }

        private int conferenceMode
        {
            get { return _conferenceMode; }
            set
            {
                switch (value)
                {
                    case 0:
                        NormalMode();
                        break;
                    case 1:
                        MultiMode();
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }

                _conferenceMode = value;
            }
        }

        public bool MainMenu
        {
            get { return mainMenu; }
            set
            {
                if (value)
                    MenuON();
                else
                    MenuOFF();
            }
        }

        #endregion


        #region Initialize        

        public Frm_Main()
        {
            if (instance == null)
            {
                instance = this;
            }
            else if (instance != this)
            {
                Close();
                return;
            }

            InitializeComponent();

            FrmInit();

            InitTVVSevent();
        }

        #endregion


        #region UI        


        #region Test        

        //Test
        private void Dbc_MouseClick(object sender, DuiMouseEventArgs e)
        {
            var dbc = sender as DuiBaseControl;
            string tag = dbc.Tag.ToString();
            bool opened = true;
            if (tag.EndsWith("#open"))
            {
                opened = true;
            }
            else
            {
                opened = false;
            }
            tag = tag.Substring(0, tag.IndexOf('#'));
            if (dbc != null)
            {
                var _items = layListBox_Contacts.Items.OfType<DuiBaseControl>().Where(o => o.Tag.ToString() == tag).ToArray();
                for (int i = 0; i < _items.Length; i++)
                {
                    _items[i].Visible = opened ? false : true;
                }
            }
            dbc.Tag = tag + (opened ? "#close" : "#open");
            layListBox_Contacts.RefreshList();
        }

        private void SetBorder(DuiBaseControl _dbc, params object[] args)
        {
            _dbc.Borders.LeftColor = (Color)args[0];
            _dbc.Borders.TopColor = (Color)args[1];
            _dbc.Borders.RightColor = (Color)args[2];
            _dbc.Borders.BottomColor = (Color)args[3];

            _dbc.Borders.LeftWidth = (int)args[4];
            _dbc.Borders.TopWidth = (int)args[5];
            _dbc.Borders.RightWidth = (int)args[6];
            _dbc.Borders.BottomWidth = (int)args[7];
        }

        #endregion


        #region OVERRIDE

        protected override void OnLoad(EventArgs e)
        {
            if (Configuration.Main_Meni_AutoOpen)
            {
                MenuON();
            }
            else
            {
                MenuOFF();
            }

            MenuSelected(layPanel_Item_Conference_Point);

            UpdateContact();

            //Test
            FillListBox(layListBox_Video_Sercher, Configuration.UserVideoList.ToArray(), new Size(200, 100), ListOrientation.Horizontal);
            FillListBox(layListBox_User_Top3, Configuration.UserVideoList.ToArray(), new Size(200, 100), ListOrientation.Horizontal);
            FillListBox(layListBox_User_Videos, Configuration.UserVideoList.ToArray(), new Size(220, 150), ListOrientation.Horizontal);

            int _c = 9;
            int _pageCount = Configuration.UserVideoList.Count / _c;
            _pageCount += (Configuration.UserVideoList.Count % _c) == 0 ? 0 : 1;
            for (int i = 0; i < _pageCount; i++)
            {
                DuiButton _db = new DuiButton();
                _db.Cursor = Cursors.Hand;
                _db.Size = new Size(12, 12);
                _db.BaseColor = Color.Yellow;
                _db.Tag = i * (3 * (220 + 20));
                _db.NormalImage = i == 0 ? TMUI.SkinImage("icon_7001_c") : TMUI.SkinImage("icon_7001_n");
                _db.HoverImage = TMUI.SkinImage("icon_7001_c");
                _db.MouseClick += (o, oe) =>
                {
                    layListBox_User_Videos.SetRollValue(-(int)_db.Tag);
                    _db.NormalImage = TMUI.SkinImage("icon_7001_c");
                    foreach (DuiButton item in layPanel_User_Videos_PageControl.DUIControls)
                    {
                        if (item != _db)
                        {
                            item.NormalImage = TMUI.SkinImage("icon_7001_n");
                        }
                    }
                };
                layPanel_User_Videos_PageControl.DUIControls.Add(_db);
            }
            int _leftOffset = (layPanel_User_Videos_PageControl.Width - (layPanel_User_Videos_PageControl.DUIControls.Count * 14)) / 2;
            foreach (DuiButton item in layPanel_User_Videos_PageControl.DUIControls)
            {
                item.Left = _leftOffset;
                _leftOffset += 14;
            }

            base.OnLoad(e);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            instance = null;

            base.OnClosed(e);

            Frm_Startup.instance.Close();
        }

        protected override void UpdateLayout()
        {
            base.UpdateLayout();

            this.BackColor = Color.Transparent;

            layPanel_Body.ImageToBackground("img_3019_n");
            layPanel_Logo.ImageToBackground("img_3020_n");

            //Menus
            SetMenuItem(layPanel_Item_Conference_Point, Configuration.StrConference, "icon_3037_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent);
            SetMenuItem(layPanel_Item_VideoLookBack_Point, Configuration.StrVideoPlayBack, "icon_3041_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent);
            SetMenuItem(layPanel_Item_Contact_Point, Configuration.StrContact, "icon_3038_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent);
            SetMenuItem(layPanel_Item_Setting_Point, Configuration.StrSetting, "icon_3039_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent);
            SetMenuItem(layPanel_Item_User_Point, Configuration.StrUser, "icon_3040_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent);
            layPanel_Menu_Handler.ImageToBackground("img_3026_n");
            layBtn_GridHandler.ImageToButtonImage("img_3025");
            layBtn_GridHandler.Cursor = Cursors.Hand;
            //layPanel_GridHandler.ImageToBackground("layBtn_GridHandler");

            //Tag Page
            //layPanel_Context_TabControl_VideoConference.ImageToBackground("Context_Bg_2");
            //layPanel_Context_TabControl_VideoLookBack.ImageToBackground("Context_Bg_2");
            //layPanel_Context_TabControl_Contact.ImageToBackground("Context_Bg_2");
            //layPanel_Context_TabControl_Setting.ImageToBackground("Context_Bg_2");
            //layPanel_Context_TabControl_User.ImageToBackground("Context_Bg_2");
            layPanel_Context.ImageToBackground("img_3012_n");
            layPanel_Context.BackgroundImageLayout = ImageLayout.Tile;

            //Video Conference
            //Playing Title
            layLab_VideoConference_Playing_Title.ImageToIcon(AlignType.Left | AlignType.Vertical, new Point(20, 0), "icon_3001_n");
            layLab_VideoConference_Playing_Title.AddIcon(AlignType.Right | AlignType.Vertical, new Point(-20, 0), "icon_3002_n");
            layLab_VideoConference_Playing_Title.TextAlign = ContentAlignment.MiddleCenter;
            layLab_VideoConference_Playing_Title.Cursor = Cursors.Hand;

            layPanel_Mutil_AddAccount.ImageToBackground("img_3011_n");
            layPanel_Mutil_AddAccount_Add.Cursor = Cursors.Hand;
            layPanel_Mutil_AddAccount_Add.ImageToIcon(AlignType.Left | AlignType.Vertical, new Point(10, 0), "icon_3005_n");
            layPanel_Mutil_AddAccount_Add.ColorMouseEnter(Color.FromArgb(100, 120, 120, 120), TMColor.Color_39f3ff);
            layPanel_Mutil_AddAccount_Add.ColorMouseLeave(TMColor.Color_Transparent, TMColor.Color_F1F1F1);
            var _tmpDuilab = layPanel_Mutil_AddAccount_Add.DUIControls.OfType<DuiLabel>().FirstOrDefault();
            _tmpDuilab.Cursor = Cursors.Hand;
            _tmpDuilab.AlignControl(AlignType.Center, Point.Empty);
            _tmpDuilab.Text = Configuration.MutilCallAddTip;
            _tmpDuilab.Size = layPanel_Mutil_AddAccount_Add.Size;
            _tmpDuilab.TextAlign = ContentAlignment.MiddleLeft;
            _tmpDuilab.Font = TMFont.GetFont("微软雅黑", 14);
            _tmpDuilab.ForeColor = TMColor.Color_F1F1F1;
            _tmpDuilab.AlignControl(AlignType.Left | AlignType.Vertical, new Point(40, 0));
            //---------------------------------------------------

            layPanel_Robot.ImageToBackground("img_3027_n");
            layPanel_Wifi.ImageToBackground("img_3028_n");
            layPanel_Context_TabControl_VideoConference_Context_Input.ImageToBackground("img_3017_n");
            layTB_Account_Input.Font = TMFont.GetFont("微软雅黑 Light", 30);
            layTB_Account_Input.WaterFont = TMFont.GetFont("微软雅黑 Light", 15);
            layTB_Account_Input.Align(2, new Point(0, -6));
            layTB_Account_Input.WaterTextOffset = new Point(0, (layTB_Account_Input.Height - layTB_Account_Input.WaterFont.Height) / 2);
            layPanel_Context_TabControl.ItemSize = new Size(1, 1);

            layBtn_VideoConference_Ready_CallingDrop.Cursor = Cursors.Hand;
            layBtn_VideoConference_Ready_CallingDrop.ImageToIcon("icon_3010_n");
            layPanel_VideoConference_Ready_CallingDrop.ImageToBackground("img_3011_n");

            layBtn_VideoCall.BackgroundImage = TMUI.SkinImage("button_3012_d");
            layBtn_VideoCall.ImageToButtonImage("button_3012");

            layBtn_VideoConference_Control_DisplayTools_Hangup.Cursor = Cursors.Hand;
            layBtn_VideoConference_Control_DisplayTools_Hangup.ImageToButtonImage("button_3002");

            layLab_Context_AddContact.Cursor = Cursors.Hand;

            layBtn_Mode_Normal.ImageToBackground("img_3023_n");
            layBtn_Mode_Multi.ImageToBackground("img_3024_n");

            layPanel_VoiceOnly_Icon.ImageToBackground("img_3002_n");

            layPanel_VideoConference_Control.BackColor = Color.FromArgb(128, 0, 0, 0);
            layPanel_VideoConference_Playing_Title.BackColor = Color.FromArgb(128, 0, 0, 0);
            layTabControl_VideoConference.ItemSize = size1;
            layTabControl_VideoConference_Control.ItemSize = size1;
            layTabControl_VideoConference_Control_Mark.ItemSize = size1;

            layCB_VideoConference_Control_Zoom.Cursor = Cursors.Hand;
            layCB_VideoConference_Control__Focus.Cursor = Cursors.Hand;
            layCB_VideoConference_Control_Mark.Cursor = Cursors.Hand;
            layCB_VideoConference_Control_TakePhoto.Cursor = Cursors.Hand;
            layCB_VideoConference_Control_Screenshot.Cursor = Cursors.Hand;
            layCB_VideoConference_Control_Volume.Cursor = Cursors.Hand;
            layCB_VideoConference_Control_Zoom.ImageToCheckBox("checkbox_3003");
            layCB_VideoConference_Control__Focus.ImageToCheckBox("checkbox_3003");
            layCB_VideoConference_Control_Mark.ImageToCheckBox("checkbox_3003");
            layCB_VideoConference_Control_TakePhoto.ImageToCheckBox("checkbox_3003");
            layCB_VideoConference_Control_Screenshot.ImageToCheckBox("checkbox_3003");
            layCB_VideoConference_Control_Volume.ImageToCheckBox("checkbox_3003");
            ccCBList.Add(layCB_VideoConference_Control_Zoom);
            ccCBList.Add(layCB_VideoConference_Control__Focus);
            ccCBList.Add(layCB_VideoConference_Control_Mark);
            ccCBList.Add(layCB_VideoConference_Control_TakePhoto);
            ccCBList.Add(layCB_VideoConference_Control_Screenshot);
            ccCBList.Add(layCB_VideoConference_Control_Volume);

            ccMarkCubeCBList.Add(layCB_Cube_1);
            ccMarkCubeCBList.Add(layCB_Cube_2);
            ccMarkCubeCBList.Add(layCB_Cube_3);
            ccMarkCubeCBList.Add(layCB_Cube_4);
            ccMarkCubeCBList.Add(layCB_Cube_5);
            ccMarkCubeCBList.Add(layCB_Cube_6);
            ccMarkCubeCBList.Add(layCB_Cube_7);
            ccMarkCubeCBList.Add(layCB_Cube_8);

            ccMarkArrCBList.Add(layCB_Arr_1);
            ccMarkArrCBList.Add(layCB_Arr_2);
            ccMarkArrCBList.Add(layCB_Arr_3);
            ccMarkArrCBList.Add(layCB_Arr_4);
            ccMarkArrCBList.Add(layCB_Arr_5);
            ccMarkArrCBList.Add(layCB_Arr_6);
            ccMarkArrCBList.Add(layCB_Arr_7);

            ccMarkSolidCBList.Add(layCB_Solid_1);
            ccMarkSolidCBList.Add(layCB_Solid_2);
            ccMarkSolidCBList.Add(layCB_Solid_3);
            ccMarkSolidCBList.Add(layCB_Solid_4);
            ccMarkSolidCBList.Add(layCB_Solid_5);

            //Tool Control
            layCB_VideoConference_Control_DisplayTools_DisplayControl_WinStateSwitch.Cursor = Cursors.Hand;
            layCB_VideoConference_Control_DisplayTools_DisplayControl_WinStateSwitch.ImageToCheckBox("checkbox_3002");
            layCB_VideoConference_Control_DisplayTools_DisplayControl_WinStateSwitch.UpdateLayout();

            layBtn_VideoConference_Control_DisplayTools_DisplayControl_GridScreen.Cursor = Cursors.Hand;
            layBtn_VideoConference_Control_DisplayTools_DisplayControl_GridScreen.ImageToButtonImage("button_3008");
            layBtn_VideoConference_Control_DisplayTools_MessageControl_Send.Cursor = Cursors.Hand;
            layBtn_VideoConference_Control_DisplayTools_MessageControl_Send.ImageToButtonImage("button_3001");
            layPanel_VideoConference_Control_DisplayTools_VolumeControl_Slider.ImageToTrackbar(new Point(0, -4), "img_3007_n", "button_3006", "img_3008_n");
            //---Zoom
            layPanel_VideoConference_Control_Zoom_Slider.ImageToTrackbar(new Point(0, 0), "img_3003_n", "button_3005", "img_3005_n");
            layPanel_VideoConference_Control_Zoom_Slider.Align(0);
            layBtn_VideoConference_Control_Zoom_Plus.Cursor = Cursors.Hand;
            layBtn_VideoConference_Control_Zoom_Plus.ImageToIcon("button_3004_n");
            layBtn_VideoConference_Control_Zoom_Less.Cursor = Cursors.Hand;
            layBtn_VideoConference_Control_Zoom_Less.ImageToIcon("button_3003_n");
            //Focus
            layCheckBox_AutoFocus.ImageToCheckBox("checkbox_3001");
            layCheckBox_AutoFocus.UpdateLayout();

            //---Mark
            layBtn_VideoConference_Control_Mark_Pre.Cursor = Cursors.Hand;
            layBtn_VideoConference_Control_Mark_Pre.ImageToIcon("icon_3011_n");
            layBtn_VideoConference_Control_Mark_Next.Cursor = Cursors.Hand;
            layBtn_VideoConference_Control_Mark_Next.ImageToIcon("icon_3010_n");
            layCB_Cube_1.Cursor = Cursors.Hand;
            layCB_Cube_1.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Cube_1.ImageToIcon("icon_3022_n");
            layCB_Cube_2.Cursor = Cursors.Hand;
            layCB_Cube_2.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Cube_2.ImageToIcon("icon_3023_n");
            layCB_Cube_3.Cursor = Cursors.Hand;
            layCB_Cube_3.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Cube_3.ImageToIcon("icon_3024_n");
            layCB_Cube_4.Cursor = Cursors.Hand;
            layCB_Cube_4.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Cube_4.ImageToIcon("icon_3025_n");
            layCB_Cube_5.Cursor = Cursors.Hand;
            layCB_Cube_5.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Cube_5.ImageToIcon("icon_3026_n");
            layCB_Cube_6.Cursor = Cursors.Hand;
            layCB_Cube_6.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Cube_6.ImageToIcon("icon_3027_n");
            layCB_Cube_7.Cursor = Cursors.Hand;
            layCB_Cube_7.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Cube_7.ImageToIcon("icon_3028_n");
            layCB_Cube_8.Cursor = Cursors.Hand;
            layCB_Cube_8.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Cube_8.ImageToIcon("icon_3029_n");

            layCB_Arr_1.Cursor = Cursors.Hand;
            layCB_Arr_1.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Arr_1.ImageToIcon("icon_3017_n");
            layCB_Arr_2.Cursor = Cursors.Hand;
            layCB_Arr_2.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Arr_2.ImageToIcon("icon_3018_n");
            layCB_Arr_3.Cursor = Cursors.Hand;
            layCB_Arr_3.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Arr_3.ImageToIcon("icon_3019_n");
            layCB_Arr_4.Cursor = Cursors.Hand;
            layCB_Arr_4.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Arr_4.ImageToIcon("icon_3020_n");
            layCB_Arr_5.Cursor = Cursors.Hand;
            layCB_Arr_5.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Arr_5.ImageToIcon("icon_3021_n");
            layCB_Arr_6.Cursor = Cursors.Hand;
            layCB_Arr_6.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Arr_6.ImageToIcon("icon_3015_n");
            layCB_Arr_7.Cursor = Cursors.Hand;
            layCB_Arr_7.ImageToCheckBoxCheckedNormalImage("icon_3035_n");
            layCB_Arr_7.ImageToIcon("icon_3016_n");

            layCB_Solid_1.Cursor = Cursors.Hand;
            layCB_Solid_1.ImageToCheckBoxCheckedNormalImage("img_3029_n");
            layCB_Solid_1.ImageToIcon("icon_3030_n");
            layCB_Solid_2.Cursor = Cursors.Hand;
            layCB_Solid_2.ImageToCheckBoxCheckedNormalImage("img_3029_n");
            layCB_Solid_2.ImageToIcon("icon_3031_n");
            layCB_Solid_3.Cursor = Cursors.Hand;
            layCB_Solid_3.ImageToCheckBoxCheckedNormalImage("img_3029_n");
            layCB_Solid_3.ImageToIcon("icon_3032_n");
            layCB_Solid_4.Cursor = Cursors.Hand;
            layCB_Solid_4.ImageToCheckBoxCheckedNormalImage("img_3029_n");
            layCB_Solid_4.ImageToIcon("icon_3033_n");
            layCB_Solid_5.Cursor = Cursors.Hand;
            layCB_Solid_5.ImageToCheckBoxCheckedNormalImage("img_3029_n");
            layCB_Solid_5.ImageToIcon("icon_3034_n");

            //Take Photo
            layBtn_TakePhoto.Cursor = Cursors.Hand;
            layBtn_TakePhoto.ImageToButtonImage("button_3010");
            layBtn_TakePhoto.Align(0);

            //Screenshot
            layBtn_ScreenShot.Cursor = Cursors.Hand;
            layBtn_ScreenShot.ImageToButtonImage("icon_3009");
            layBtn_ScreenShot.Align(0);

            DefaultMark();
            ShowConferenceState(0);


            #region Contact

            layTabControl_Contacts.ItemSize = size1;
            layBtn_JumpContact.Cursor = Cursors.Hand;

            layPanel_Icon_Confirm_166.ImageToBackground("img_3021_n");
            layBtn_Contact_Add_Carryon.ImageToButtonImage("button_3013");

            #endregion


            #region Video Lookback

            layPanel_VideoControl.BackColor = Color.FromArgb(100, 0, 0, 0);
            layPanel_VideoSercher.BackColor = Color.FromArgb(100, 0, 0, 0);

            layCheckBox_Video_StateSwitch.ImageToCheckBox("checkbox_3002");
            layCheckBox_Video_StateSwitch.Checked = !layCheckBox_Video_StateSwitch.Checked;

            layTrackBar_Video_Volume.ImageToTrackbar(new Point(0, -4), "img_3007_n", "button_3006", "img_3008_n");

            layeredPanel1.ImageToBackground("img_4001_n");
            layeredPanel2.ImageToBackground("img_4001_n");
            layeredPanel3.ImageToBackground("img_4001_n");
            layeredLabel2.ForeColor = TMColor.Color_F1F1F1;
            layeredLabel12.ForeColor = TMColor.Color_F1F1F1;
            layeredLabel13.ForeColor = TMColor.Color_F1F1F1;

            layeredButton2.Cursor = Cursors.Hand;
            layeredButton3.Cursor = Cursors.Hand;
            layeredButton4.Cursor = Cursors.Hand;
            layeredButton4.AddIcon(AlignType.Center, Point.Empty, "icon_4006_n");

            #endregion


            #region Setting

            SetDuiCheckBox(layPanel_Setting_Login, "checkbox_5001", TMColor.Color_F1F1F1);
            SetDuiCheckBox(layPanel_Setting_Board, "checkbox_5001", TMColor.Color_F1F1F1);
            SetDuiCheckBox(layPanel_Setting_Notify, "checkbox_5001", TMColor.Color_F1F1F1);
            SetDuiCheckBox(layPanel_Setting_Update, "checkbox_5001", TMColor.Color_F1F1F1);
            SetDuiCheckBox(layPanel_Setting_Capture, "checkbox_5001", TMColor.Color_F1F1F1);
            layBtn_Setting_Update.ImageToButtonImage("button_6001");
            SetChildForeColor(layPanel_Setting_Login, TMColor.Color_F1F1F1);
            SetChildForeColor(layPanel_Setting_Board, TMColor.Color_F1F1F1);
            SetChildForeColor(layPanel_Setting_Notify, TMColor.Color_F1F1F1);
            SetChildForeColor(layPanel_Setting_Update, TMColor.Color_F1F1F1);
            SetChildForeColor(layPanel_Setting_Capture, TMColor.Color_F1F1F1);

            layBtn_Setting_Update_Confrim.ImageToButtonImage("button_6001");
            layPanel_Setting_Update_Icon.ImageToBackground("img_6003_n");
            layLab_Setting_UpdatePross.ForeColor = TMColor.Color_F1F1F1;
            layLab_Setting_UpdateTip.ForeColor = TMColor.Color_F1F1F1;

            layTabControl_Setting.ItemSize = size1;

            layPanel_Setting_NonUpdate_icon.ImageToBackground("img_6002_n");
            layPanel_Setting_NonUpdate_icon.SizeToBG();
            layPanel_Setting_NonUpdate_icon.AlignControl(AlignType.Center, AnchorStyles.None, Point.Empty);

            layLab_Setting_NoneUpdate.ForeColor = TMColor.Color_F1F1F1;
            layLab_Setting_NoneUpdate.AlignControl(AlignType.Center, AnchorStyles.None, new Point(0, (layPanel_Setting_NonUpdate_icon.Height / 2) + 20));

            #endregion


            #region User

            layTabControl_User.ItemSize = size1;

            layPanel_User_Information_Avata.ImageToBackground("img_7003_n");
            layPanel_User_Information_Avata.SizeToBG();
            layPanel_User_Information_Avata.AlignControl(AlignType.Center, AnchorStyles.None, Point.Empty);

            layBtn_User_Related_More.Cursor = Cursors.Hand;
            layBtn_User_Related_More.ImageToIcon("icon_3010_n");
            layBtn_User_Related_More.BaseColor = TMColor.Color_Transparent;

            layBtn_User_Videos_Next.Cursor = Cursors.Hand;
            layBtn_User_Videos_Next.ImageToIcon("icon_3010_n");
            layBtn_User_Videos_Next.BaseColor = TMColor.Color_Transparent;

            layLab_User_Related_Title.ForeColor = TMColor.Color_F1F1F1;
            layLab_User_Videos.ForeColor = TMColor.Color_F1F1F1;

            layLab_User_Information_Name.AlignControl(AlignType.Bottom | AlignType.Horizontal, AnchorStyles.None, new Point(0, -40));
            layLab_User_Information_Name.ForeColor = TMColor.Color_F1F1F1;

            #endregion


            SetControlBoxButton(true, true, true);
        }

        protected override void InitEvent()
        {
            base.InitEvent();

            //Menu
            layPanel_Item_Conference_Point.MouseEnter += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrConference, "icon_3037_h", TMColor.Color_39f3ff, TMColor.Color_252628); };
            layPanel_Item_Conference_Point.MouseLeave += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrConference, "icon_3037_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent); };
            layPanel_Item_Conference_Point.Click += LayPanel_Menu_Item_Click;

            layPanel_Item_VideoLookBack_Point.MouseEnter += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrVideoPlayBack, "icon_3041_h", TMColor.Color_39f3ff, TMColor.Color_252628); };
            layPanel_Item_VideoLookBack_Point.MouseLeave += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrVideoPlayBack, "icon_3041_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent); };
            layPanel_Item_VideoLookBack_Point.Click += LayPanel_Menu_Item_Click;

            layPanel_Item_Contact_Point.MouseEnter += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrContact, "icon_3038_h", TMColor.Color_39f3ff, TMColor.Color_252628); };
            layPanel_Item_Contact_Point.MouseLeave += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrContact, "icon_3038_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent); };
            layPanel_Item_Contact_Point.Click += LayPanel_Menu_Item_Click;

            layPanel_Item_Setting_Point.MouseEnter += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrSetting, "icon_3039_h", TMColor.Color_39f3ff, TMColor.Color_252628); };
            layPanel_Item_Setting_Point.MouseLeave += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrSetting, "icon_3039_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent); };
            layPanel_Item_Setting_Point.Click += LayPanel_Menu_Item_Click;

            layPanel_Item_User_Point.MouseEnter += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrUser, "icon_3040_h", TMColor.Color_39f3ff, TMColor.Color_252628); };
            layPanel_Item_User_Point.MouseLeave += (o, e) => { SetMenuItem(o as LayeredPanel, Configuration.StrUser, "icon_3040_n", TMColor.Color_F1F1F1, TMColor.Color_Transparent); };
            layPanel_Item_User_Point.Click += LayPanel_Menu_Item_Click;

            layBtn_GridHandler.Click += LayBtn_GridHandler_Click;

            //Logo
            layPanel_Logo.MouseDown += Frm_MouseDown;

            #region Video Conference

            layLab_VideoConference_Playing_Title.Click += (o, e) => { PlayingTitleClick(); };

            //Input
            layTB_Account_Input.TextChanged += LayTB_Account_Input_TextChanged;
            layBtn_VideoConference_Ready_CallingDrop.Click += (o, e) => { layPanel_VideoConference_Ready_CallingDrop.Visible = !layPanel_VideoConference_Ready_CallingDrop.Visible; };
            layPanel_VideoConference_Playing_Title.VisibleChanged += (o, e) =>
            {
                if (!((LayeredPanel)o).Visible)
                {
                    layPanel_Mutil_AddAccount.Visible = false;
                }
            };

            //Mode
            layBtn_Mode_Normal.Click += LayBtn_Mode_Normal_Click;
            layBtn_Mode_Multi.Click += LayBtn_Mode_Multi_Click;

            //Conference Call
            layBtn_VideoCall.Click += LayBtn_VideoCall_Click;
            layLab_Context_AddContact.Click += (o, e) => { LayPanel_Menu_Item_Click(layPanel_Item_Contact_Point, null); layTabControl_Contacts.SelectedIndex = 1; };

            layCB_VideoConference_Control_Zoom.Click += (o, e) => { ShowConferenceControl(1); ConferenceControlClick(((LayeredCheckButton)o)); };
            layCB_VideoConference_Control__Focus.Click += (o, e) => { ShowConferenceControl(2); ConferenceControlClick(((LayeredCheckButton)o)); };
            layCB_VideoConference_Control_Mark.Click += (o, e) => { ShowConferenceControl(3); ConferenceControlClick(((LayeredCheckButton)o)); };
            layCB_VideoConference_Control_TakePhoto.Click += (o, e) => { ShowConferenceControl(4); ConferenceControlClick(((LayeredCheckButton)o)); };
            layCB_VideoConference_Control_Screenshot.Click += (o, e) => { ShowConferenceControl(5); ConferenceControlClick(((LayeredCheckButton)o)); };
            layCB_VideoConference_Control_Volume.Click += (o, e) => { ShowConferenceControl(6); ConferenceControlClick(((LayeredCheckButton)o)); };

            var tabPages = layTabControl_VideoConference_Control_Mark.TabPages.OfType<TabPage>().ToArray();
            foreach (LayeredCheckButton item in tabPages[0].Controls)
            {
                item.Click += (o, e) => { SelectCube(((LayeredCheckButton)o)); };
            }
            foreach (LayeredCheckButton item in tabPages[1].Controls)
            {
                item.Click += (o, e) => { SelectArrow(((LayeredCheckButton)o)); };
            }
            foreach (LayeredCheckButton item in tabPages[2].Controls)
            {
                item.Click += (o, e) => { SelectSolid(((LayeredCheckButton)o)); };
            }
            layTabControl_VideoConference_Control_Mark.SelectedIndexChanged += (o, e) =>
            {
                int _index = layTabControl_VideoConference_Control_Mark.SelectedIndex;
                int _count = layTabControl_VideoConference_Control_Mark.TabCount;

                if (_index == 0)
                {
                    layBtn_VideoConference_Control_Mark_Pre.Hide();
                    layBtn_VideoConference_Control_Mark_Next.Show();
                }
                else if (_index == _count - 1)
                {
                    layBtn_VideoConference_Control_Mark_Next.Hide();
                    layBtn_VideoConference_Control_Mark_Pre.Show();
                }
                else
                {
                    layBtn_VideoConference_Control_Mark_Pre.Show();
                    layBtn_VideoConference_Control_Mark_Next.Show();
                }
            };

            layBtn_VideoConference_Control_Mark_Pre.Click += (o, e) =>
            {
                int ci = layTabControl_VideoConference_Control_Mark.SelectedIndex;
                ci--;
                layTabControl_VideoConference_Control_Mark.SelectedIndex = ci;

            };
            layBtn_VideoConference_Control_Mark_Next.Click += (o, e) =>
            {
                int ci = layTabControl_VideoConference_Control_Mark.SelectedIndex;
                ci++;
                layTabControl_VideoConference_Control_Mark.SelectedIndex = ci;
            };

            layBtn_VideoConference_Control_DisplayTools_Hangup.Click += (o, e) => { Hangup(); };

            layPanel_VideoConference_Remote.Click += (o, e) => { DisplayStatusBar(!isStatusON); };
            layPanel_Display.Click += (o, e) => { DisplayStatusBar(!isStatusON); };
            layPanel_VoiceOnly.Click += (o, e) => { DisplayStatusBar(!isStatusON); };

            #endregion


            #region Contact

            layBtn_JumpContact.MouseEnter += (o, e) => { ((LayeredLabel)o).SetBackground(Color.FromArgb(100, 120, 120, 120)); };
            layBtn_JumpContact.MouseLeave += (o, e) => { ((LayeredLabel)o).SetBackground(TMColor.Color_Transparent); };
            layBtn_JumpContact.Click += (o, e) => { MenuSelected(layPanel_Item_Contact_Point); layTabControl_Contacts.SelectedIndex = 1; };
            layBtn_JumpContact.Cursor = Cursors.Hand;

            layBtn_SelectAll.MouseEnter += (o, e) => { ((LayeredLabel)o).SetBackground(Color.FromArgb(100, 120, 120, 120)); };
            layBtn_SelectAll.MouseLeave += (o, e) => { ((LayeredLabel)o).SetBackground(TMColor.Color_Transparent); };
            layBtn_SelectAll.Cursor = Cursors.Hand;

            layBtn_Delete.MouseEnter += (o, e) => { ((LayeredLabel)o).SetBackground(Color.FromArgb(100, 120, 120, 120)); };
            layBtn_Delete.MouseLeave += (o, e) => { ((LayeredLabel)o).SetBackground(TMColor.Color_Transparent); };
            layBtn_Delete.Cursor = Cursors.Hand;
            layBtn_Delete.Click += (o, e) => { Helper.ShowDialogWindow<Frm_Contact_Delete>(this); };

            layBtn_ContactAdd.Click += (o, e) => { layTabControl_Contacts.SelectedIndex = 2; };

            layBtn_ContactAddCancel.Click += (o, e) => { layTabControl_Contacts.SelectedIndex = 0; };

            layBtn_Contact_Add_Carryon.Click += (o, e) => { layTabControl_Contacts.SelectedIndex = 1; };

            #endregion



            #region Setting

            layBtn_Setting_Update.Click += (o, e) => { ShowTabPage(layTabControl_Setting, 1); };

            layBtn_Setting_Update_Confrim.Click += (o, e) => { ShowTabPage(layTabControl_Setting, 2); };

            #endregion


            #region User

            layBtn_User_Related_More.Click += (o, e) => { ShowTabPage(layTabControl_User, 1); };

            layBtn_User_Videos_Next.Click += (o, e) => { layListBox_User_Videos.SetRollValue((new Random()).Next(10)); };

            #endregion
        }

        #endregion


        #region PRIVATE_METHOD

        private void SetMenuItem(LayeredPanel lp, string title, string _icon, Color _foreColor, Color _bgColor)
        {
            if (lp == null || lp == selectedItem)
                return;

            lp.BackColor = _bgColor;
            lp.Cursor = Cursors.Hand;

            lp.ImageToIcon(AlignType.Left, new Point(10, 0), _icon);

            var icon = lp.DUIControls.OfType<DuiPictureBox>().FirstOrDefault();
            if (icon != null)
            {
                icon.Cursor = Cursors.Hand;
            }

            var lab = lp.DUIControls.OfType<DuiLabel>().FirstOrDefault();
            if (lab != null)
            {
                lab.ForeColor = _foreColor;
                if (title != null)
                    lab.Text = title;
                lab.Cursor = Cursors.Hand;
            }
        }

        private void OpenContext<T>() where T : Form, new()
        {

        }

        private void MenuSelected(int index)
        {
            OpenContext(index);
        }

        private void MenuSelected(LayeredPanel lp)
        {
            selectedItem = lp;

            if (lp == layPanel_Item_Conference_Point)
            {
                layPanel_Company_Information.Show();
                MenuSelected(0);
                NormalMode();
            }
            else if (lp == layPanel_Item_VideoLookBack_Point)
            {
                MenuSelected(1);
                layPanel_Company_Information.Hide();
            }
            else if (lp == layPanel_Item_Contact_Point)
            {
                layPanel_Company_Information.Show();
                MenuSelected(2);
                layTabControl_Contacts.SelectedIndex = 0;
            }
            else if (lp == layPanel_Item_Setting_Point)
            {
                layPanel_Company_Information.Show();
                MenuSelected(3);
                ShowTabPage(layTabControl_Setting, 0);
            }
            else if (lp == layPanel_Item_User_Point)
            {
                layPanel_Company_Information.Show();
                MenuSelected(4);
                ShowTabPage(layTabControl_User, 0);

                //Test

            }
        }

        private void CallActive(bool active)
        {
            layBtn_VideoCall.Enabled = active;

            if (active)
            {
                layBtn_VideoCall.ImageToNormalImage("button_3012_n");
            }
            else
            {
                layBtn_VideoCall.NormalImage = null;
            }
        }

        private void NormalMode()
        {
            var lab = layBtn_Mode_Normal.DUIControls.OfType<DuiLabel>().FirstOrDefault();
            if (lab != null)
            {
                lab.ForeColor = TMColor.Color_39f3ff;
                lab.Font = new Font(lab.Font, FontStyle.Bold);
            }
            lab = layBtn_Mode_Multi.DUIControls.OfType<DuiLabel>().FirstOrDefault();
            if (lab != null)
            {
                lab.ForeColor = TMColor.Color_Gry210;
                lab.Font = new Font(lab.Font.FontFamily, lab.Font.Size);
            }
        }

        private void MultiMode()
        {
            var lab = layBtn_Mode_Normal.DUIControls.OfType<DuiLabel>().FirstOrDefault();
            if (lab != null)
            {
                lab.ForeColor = TMColor.Color_Gry210;
                lab.Font = new Font(lab.Font.FontFamily, lab.Font.Size);
            }
            lab = layBtn_Mode_Multi.DUIControls.OfType<DuiLabel>().FirstOrDefault();
            if (lab != null)
            {
                lab.ForeColor = TMColor.Color_39f3ff;
                lab.Font = new Font(lab.Font, FontStyle.Bold);
            }
        }

        private void CallIDCheck()
        {
            var ret = Helper.CheckCallID(layTB_Account_Input.Text);

            if (ret == 0)
            {
                CallActive(true);
            }
            else
            {
                CallActive(false);
            }
        }

        private string GetCallingID()
        {
            return layTB_Account_Input.Text.Trim();
        }

        private void ShowConferenceState(int index)
        {
            layTabControl_VideoConference.SelectedIndex = index;

            if (index > 0)
            {
                layPanel_Company_Information.Hide();
                layPanel_Mutil_AddAccount.Hide();
            }
            else
            {
                layPanel_Company_Information.Show();
            }
        }

        private void ConferenceControlClick(LayeredCheckButton _lcb)
        {
            if (_lcb.Checked)
            {
                _lcb.Checked = false;
                ShowConferenceControl(0);
            }
            else
            {
                _lcb.Checked = true;
                ccCBList.ForEach(o =>
                {
                    if (o != _lcb)
                        o.Checked = false;
                });
            }
        }

        private void ShowConferenceControl(int index1, int index2 = 0)
        {
            layTabControl_VideoConference_Control.SelectedIndex = index1;
            var tabPage = layTabControl_VideoConference_Control.SelectedTab;
            var childTabControl = tabPage.Controls.OfType<LayeredTabControl>().FirstOrDefault();
            if (childTabControl != null)
            {
                childTabControl.SelectedIndex = index2;
            }
        }

        private void SelectCube(int index)
        {

        }

        private void SelectCube(LayeredCheckButton _lcb)
        {
            if (_lcb == null)
            {
                ccMarkCubeCBList.ForEach(o =>
                {
                    o.Checked = false;
                });
                return;
            }

            if (_lcb.Checked)
                return;

            _lcb.Checked = true;

            ccMarkCubeCBList.ForEach(o =>
            {
                if (o != _lcb)
                {
                    o.Checked = false;
                }
            });
        }

        private void SelectArrow(int index)
        {

        }

        private void SelectArrow(LayeredCheckButton _lcb)
        {
            if (_lcb == null)
            {
                ccMarkArrCBList.ForEach(o =>
                {
                    o.Checked = false;
                });
                return;
            }

            if (_lcb.Checked)
                return;

            _lcb.Checked = true;

            ccMarkArrCBList.ForEach(o =>
            {
                if (o != _lcb)
                {
                    o.Checked = false;
                }
            });
        }

        private void SelectSolid(int index)
        {

        }

        private void SelectSolid(LayeredCheckButton _lcb)
        {
            if (_lcb == null)
            {
                ccMarkSolidCBList.ForEach(o =>
                {
                    o.Checked = false;
                });
                return;
            }

            if (_lcb.Checked)
                return;

            _lcb.Checked = true;

            ccMarkSolidCBList.ForEach(o =>
            {
                if (o != _lcb)
                {
                    o.Checked = false;
                }
            });
        }

        private void DefaultMark()
        {
            SelectCube(ccMarkCubeCBList[3]);
            SelectArrow(null);
            SelectSolid(ccMarkSolidCBList[0]);
            layBtn_VideoConference_Control_Mark_Pre.Hide();
            layBtn_VideoConference_Control_Mark_Next.Show();
        }

        private void DisplayStatusBar(bool _switch)
        {
            isStatusON = _switch;
            layPanel_VideoConference_Control.Visible = _switch;
            layPanel_VideoConference_Playing_Title.Visible = _switch;
        }

        //Video lookback



        //Contact

        private void UpdateContact()
        {
            UpdateContactList(layListBox_Contacts);
            DropDown(layListBox_VideoConference_Ready_CallingDrop, Configuration.ContactList.ToArray());
            layPanel_VideoConference_Ready_CallingDrop.Hide();
        }

        private void UpdateContactList(LayeredListBox _llb)
        {
            var itemsGroup = Helper.GroupItems(Configuration.ContactList).ToArray();

            for (int i = 0; i < itemsGroup.Length; i++)
            {
                DuiBaseControl dbc = new DuiBaseControl();
                dbc.Width = _llb.Width;
                dbc.Height = 52;
                dbc.Name = itemsGroup[i].Key;
                dbc.MouseClick += Dbc_MouseClick;
                dbc.BackColor = Color.FromArgb(128, 120, 120, 120);
                dbc.Tag = "group" + dbc.Name + "#open";
                dbc.Cursor = Cursors.Hand;
                SetBorder(dbc, Color.Silver, Color.Silver, Color.Silver, Color.Silver, 2, 2, 2, 2);
                dbc.MouseEnter += (o, e) =>
                {
                    Helper.SetBorders(o as DuiBaseControl, Color.White, Color.White, Color.White, Color.White, 2, 2, 2, 2);
                };
                dbc.MouseLeave += (o, e) =>
                {
                    Helper.SetBorders(o as DuiBaseControl, Color.Silver, Color.Silver, Color.Silver, Color.Silver, 2, 2, 2, 2);
                };

                DuiLabel _title = new DuiLabel();
                _title.TextAlign = ContentAlignment.MiddleLeft;
                _title.Text = dbc.Name;
                _title.Location = new Point(10, (dbc.Height - _title.Height) / 2);
                _title.Font = TMFont.GetFont("微软雅黑", 12);
                _title.ForeColor = Color.White;

                dbc.Controls.Add(_title);

                _llb.Items.Add(dbc);

                var tmp = itemsGroup[i].ToArray();

                for (int k = 0; k < tmp.Length; k++)
                {
                    DuiBaseControl _dbc = new DuiBaseControl();
                    _dbc.Width = _llb.Width;
                    _dbc.Height = 52;
                    _dbc.Name = "item" + k;
                    _dbc.Tag = dbc.Tag.ToString().Substring(0, dbc.Tag.ToString().IndexOf('#'));
                    _dbc.Cursor = Cursors.Hand;

                    _dbc.MouseEnter += (o, e) =>
                    {
                        _dbc.BackColor = Color.FromArgb(100, 120, 120, 120);
                    };
                    _dbc.MouseLeave += (o, e) =>
                    {
                        _dbc.BackColor = TMColor.Color_Transparent;
                    };

                    DuiCheckBox dcb = new DuiCheckBox();
                    dcb.Text = tmp[k] + " " + _dbc.Tag.ToString();
                    dcb.Tag = tmp[k];
                    dcb.ForeColor = Color.White;
                    dcb.Font = TMFont.GetFont("微软雅黑", 12);
                    dcb.Location = new Point(10, (_dbc.Height - dcb.Height) / 2);
                    dcb.SpaceBetweenCheckMarkAndText = 50;
                    dcb.Cursor = Cursors.Hand;
                    dcb.CheckedChanged += (o, e) =>
                    {
                        if (dcb.Checked)
                        {
                            Configuration.AddAccountToDeleteList(dcb.Tag as string);
                        }
                        else
                        {
                            Configuration.RemoveAccountInDeleteList(dcb.Tag as string);
                        }
                    };

                    _dbc.Controls.Add(dcb);

                    _llb.Items.Add(_dbc);
                }
            }
            _llb.RefreshList();
        }

        private void DropDown(LayeredListBox _llb, string[] _items)
        {
            for (int i = 0; i < _items.Length; i++)
            {
                DuiBaseControl _dbc = new DuiBaseControl();
                DuiCheckBox _dcb = new DuiCheckBox();
                DuiLabel _lab = new DuiLabel();
                _dbc.Controls.Add(_dcb);
                _dbc.Controls.Add(_lab);
                _dbc.Tag = new List<string>();

                _dbc.Cursor = Cursors.Hand;
                _dbc.Width = _llb.Width - _llb.Padding.Left - _llb.Padding.Right;
                _dbc.Height = 42;
                _dbc.ColorMouseEnter(Color.FromArgb(100, 120, 120, 120), TMColor.Color_39f3ff, Color.Transparent);
                _dbc.ColorMouseLeave(TMColor.Color_Transparent, Color.White, Color.Transparent);

                _dcb.Enabled = false;
                _dcb.Cursor = Cursors.Hand;
                _dcb.ImageToCheckBox("checkbox_5001");
                _dcb.Tag = _items[i];

                _dcb.Size = _dcb.CheckedNormal.Size;
                _dcb.ClientRectangle = new Rectangle(0, 0, _dcb.CheckedNormal.Size.Width, _dcb.CheckedNormal.Size.Height);
                _dcb.CheckRectWidth = _dcb.CheckedNormal.Size.Width;
                _dcb.AlignControl(AlignType.Left | AlignType.Vertical, new Point(10, 0));
                _dcb.CheckedChanged += (o, e) =>
                {
                    //Todo: 
                    //    var value = _dcb.Tag as string;                    
                    //    if (_dcb.Checked)
                    //    {

                    //        if (!((List<string>)_dbc.Tag).Contains(value))
                    //        {
                    //            ((List<string>)_dbc.Tag).Add(value);
                    //        }
                    //    }
                    //    else
                    //    {
                    //        if (((List<string>)_dbc.Tag).Contains(value))
                    //        {
                    //            ((List<string>)_dbc.Tag).Remove(value);

                    //        }
                    //    };
                    //    Debug.WriteLine(((List<string>)_dbc.Tag).Count + " : " + value);
                };

                _lab.Cursor = Cursors.Hand;
                _lab.Width = _dbc.Width - _dcb.Width;
                _lab.Height = _dbc.Height;
                _lab.Font = TMFont.GetFont("微软雅黑", 14);
                _lab.ForeColor = Color.White;
                _lab.Text = _dcb.Tag as string;
                _lab.Tag = TMColor.Color_39f3ff;
                _lab.ColorMouseEnter(TMColor.Color_Transparent, TMColor.Color_39f3ff, TMColor.Color_Transparent);
                _lab.ColorMouseLeave(TMColor.Color_Transparent, Color.White, TMColor.Color_Transparent);
                _lab.MouseLeave += (o, e) =>
                {
                    if (_dcb.Checked)
                    {
                        _lab.ForeColor = TMColor.Color_39f3ff;
                    }
                };
                _lab.TextAlign = ContentAlignment.MiddleLeft;

                var evi = _lab.GetType().GetEvent("MouseLeave", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                var evif = evi.DeclaringType.GetField("MouseLeave", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                if (evif != null)
                {
                    evif.SetValue(_lab, null);
                }

                _lab.AlignControl(AlignType.Left | AlignType.Vertical, new Point(50, 0));
                _dbc.MouseClick += (o, e) =>
                {
                    _dcb.Checked = !_dcb.Checked;
                };
                _llb.Items.Add(_dbc);
            };
            _llb.RefreshList();
        }



        private void PlayingTitleClick()
        {
            var _show = layPanel_Mutil_AddAccount.Visible;
            if (_show)
            {
                layPanel_Mutil_AddAccount.Visible = false;
            }
            else
            {
                layPanel_Mutil_AddAccount.Visible = true;

                layListBox_Mutil_Account.Items.Clear();
                layListBox_Mutil_Account.RefreshList();
                DropDown(layListBox_Mutil_Account, Configuration.MutilCallList.ToArray());
            }
        }


        #region Setting

        private void SetDuiCheckBox(LayeredPanel _lp, string _icon, Color _fcolor)
        {
            foreach (LayeredBaseControl _lbc in _lp.Controls.OfType<LayeredBaseControl>())
            {
                foreach (DuiCheckBox _dcb in _lbc.DUIControls.OfType<DuiCheckBox>())
                {
                    _dcb.Cursor = Cursors.Hand;
                    _dcb.ForeColor = _fcolor;
                    _dcb.ImageToCheckBox(_icon);
                    if (_dcb.CheckedNormal != null)
                    {
                        _dcb.CheckRectWidth = _dcb.CheckedNormal.Size.Width;
                    }
                }
            }
        }

        private void SetDuiCheckBox(DuiBaseControl _dbc, string _icon, Color _fcolor)
        {
            foreach (DuiCheckBox _dcb in _dbc.Controls.OfType<DuiCheckBox>())
            {
                _dcb.Cursor = Cursors.Hand;
                _dcb.ForeColor = _fcolor;
                _dcb.ImageToCheckBox(_icon);
                if (_dcb.CheckedNormal != null)
                {
                    _dcb.CheckRectWidth = _dcb.CheckedNormal.Size.Width;
                }
            }
        }

        private void SetChildForeColor(Control _ctl, Color _fcolor)
        {
            foreach (Control item in _ctl.Controls)
            {
                item.ForeColor = _fcolor;
            }
        }


        #endregion


        #region Unival

        private void ShowTabPage(LayeredTabControl _ltc, int _index)
        {
            int _count = _ltc.TabPages.Count;

            if (_count == 0)
                return;

            _index = _index < 0 ? 0 : _index >= _count ? _count - 1 : _index;

            _ltc.SelectedIndex = _index;
        }

        private void FillListBox(LayeredListBox _llb, string[] _items, Size _itemSize, ListOrientation _lo = ListOrientation.Vertical)
        {
            _llb.Orientation = _lo;
            int _offset = 20;
            Size _rootSize = new Size(_itemSize.Width + _offset, _itemSize.Height + _offset);
            _llb.ItemSize = _rootSize;
            Random _random = new Random();
            for (int i = 0; i < _items.Length; i++)
            {
                DuiBaseControl _dbc = new DuiBaseControl();
                _dbc.Size = _rootSize;

                DuiButton _db = new DuiButton();
                _db.Text = _items[i];
                _db.Size = _itemSize;
                _db.BaseColor = Color.FromArgb(_random.Next(2) * 255, _random.Next(2) * 255, _random.Next(2) * 255);

                _dbc.Controls.Add(_db);
                _db.AlignControl(AlignType.Center, Point.Empty);

                _llb.Items.Add(_dbc);
            }
            _llb.RefreshList();
        }

        #endregion


        #endregion


        #region PUBLIC_METHOD

        public void MenuON()
        {
            MenuAnimation(true);
            //Zoom
            layPanel_VideoConference_Control_Zoom_Slider.ImageToTrackbar(new Point(0, 0), "img_3004_n", "button_3005", "img_3006_n");
            layPanel_VideoConference_Control_Zoom_Slider.Align(0);
        }

        public void MenuOFF()
        {
            MenuAnimation(false);
            //Zoom
            layPanel_VideoConference_Control_Zoom_Slider.ImageToTrackbar(new Point(0, 0), "img_3003_n", "button_3005", "img_3005_n");
            layPanel_VideoConference_Control_Zoom_Slider.Align(0);
        }

        private void MenuAnimation(bool _switch)
        {
            if (isMenuAnimation)
                return;

            isMenuAnimation = true;

            int handlerEndLeft = _switch ? 321 : 1;
            int menuWidth = _switch ? 320 : 0;
            //int offsetPS = _switch ? 3 : -3;

            //while (true)
            //{
            //    if (_switch)
            //    {
            //        if (layPanel_GridHandler.Left >= handlerEndLeft & layPanel_Menu.Width >= menuWidth)
            //        {
            //            break;
            //        }
            //    }
            //    else
            //    {
            //        if (layPanel_GridHandler.Left <= handlerEndLeft & layPanel_Menu.Width <= menuWidth)
            //        {
            //            break;
            //        }
            //    }

            //    layPanel_GridHandler.Left += offsetPS;
            //    layPanel_Menu.Width += offsetPS;

            //    await Task.Delay(6);
            //}

            layPanel_GridHandler.Left = handlerEndLeft;
            layPanel_Menu.Width = menuWidth;

            mainMenu = _switch;

            layPanel_Body.Hide();
            layPanel_Body.Show();

            isMenuAnimation = false;
        }

        public void OpenContext(int index)
        {
            if (index < 0 || index >= layPanel_Context_TabControl.TabCount)
                return;

            layPanel_Context_TabControl.SelectedIndex = index;
            CallIDCheck();
        }

        public void VideoCall()
        {
            VideoCall(GetCallingID());
        }

        public void ConferenceReady()
        {
            ShowConferenceState(0);
        }

        public void ConferenceDisplay()
        {
            ShowConferenceState(1);
        }

        #endregion


        #region CONTROL_EVENT

        //private void LayPanel_Menu_Item_MouseLeave(object sender, EventArgs e)
        //{
        //    if (sender == null)
        //        return;

        //    var p = sender as LayeredPanel;

        //    if (p == selectedItem)
        //        return;

        //    if (p == null)
        //        return;

        //    p.BackColor = TMColor.Color_Transparent;

        //    var icon = p.DUIControls.OfType<DuiPictureBox>().FirstOrDefault();
        //    if (icon != null)
        //        icon.BackgroundImage = TMUI.SkinImage(icon.Name);

        //    var lab = p.DUIControls.OfType<DuiLabel>().FirstOrDefault();
        //    if (lab != null)
        //        lab.ForeColor = TMColor.Color_F1F1F1;
        //}

        private void LayBtn_GridHandler_Click(object sender, EventArgs e)
        {
            MainMenu = !MainMenu;
        }

        private void LayPanel_Menu_Item_Click(object sender, EventArgs e)
        {
            var lp = sender as LayeredPanel;

            MenuSelected(lp);
        }

        private void LayTB_Account_Input_TextChanged(object sender, EventArgs e)
        {
            CallIDCheck();
        }

        private void LayBtn_Mode_Multi_Click(object sender, EventArgs e)
        {
            conferenceMode = 1;
        }

        private void LayBtn_Mode_Normal_Click(object sender, EventArgs e)
        {
            conferenceMode = 0;
        }

        private void LayBtn_VideoCall_Click(object sender, EventArgs e)
        {
            VideoCall();
        }

        #endregion

        #endregion


        #region CLIENT


        #region EVENT

        //Test

        static IntPtr frameData;

        private void InitTVVSevent()
        {
            TVVS.VideoCalling += TVVS_VideoCalling;
            SetDefultDisplayAndCaptures();

            ShowFrame();
        }

        //Test

        async void ShowFrame()
        {
            while (true)
            {
                if (this.IsDisposed)
                {
                    break;
                }

                await Task.Delay(1000);
                if (TVVS.FrameData != IntPtr.Zero)
                {
                    byte[] _imgByte = new byte[Marshal.SizeOf(TVVS.FrameData)];
                    for (int i = 0; i < _imgByte.Length; i++)
                    {
                        _imgByte[i] = Marshal.ReadByte(TVVS.FrameData, i);
                    }

                    MemoryStream ms = new MemoryStream(_imgByte);

                    panel1.BackgroundImage = Image.FromStream(ms);
                }

            }
        }

        #endregion


        #region PRIVATE_METHOD

        private void ConferenceCall()
        {
            string _account = layTB_Account_Input.Text.Trim();

            VideoCall(_account);
        }

        private void VideoCall(string _account)
        {
            Configuration.CallingID = _account;

            //SetDefultDisplayAndCaptures();

            TVVS.SetFrameData(frameData);

            Helper.ShowWindow<Frm_Calling>(this);
        }

        private void AcceptCall(string _callID)
        {
            ConferenceDisplay();

            //SetDefultDisplayAndCaptures();

            TVVS.SetFrameData(frameData);

            TVVS.AcceptCall(_callID, VideoCallResult);
        }

        private void SetDefultDisplayAndCaptures()
        {
            int _remoteHwnd = Helper.IntPtrToInt(layPanel_Display.DUIControls.OfType<DuiPictureBox>().FirstOrDefault().Handle);
            int _localHwnd = Helper.IntPtrToInt(layeredBaseControl2.Handle);
            SetupVideo(_remoteHwnd, _localHwnd);
            SetupCapture(0, 0, 0);

            TVVS.ApplyCaptures();
        }

        private void DisConnected()
        {
            Hangup();
        }

        private void Hangup()
        {
            TVVS.VideoCallCancel(null);

            ConferenceReady();
        }

        #endregion


        #region PUBLIC_METHOD

        public void SetupVideo(int _remoteHwnd, int _localHwnd)
        {
            Debug.WriteLine("remote Hwnd:" + _remoteHwnd);
            Debug.WriteLine("local Hwnd:" + _localHwnd);

            TVVS.SetScreen(_remoteHwnd, _localHwnd);
        }

        public void SetupCapture(int _cam, int _spk, int _mic, string _rsl = "AUTO")
        {
            TVVS.SetCaptures(_cam, _spk, _mic, _rsl);
        }

        public void VideoConnected()
        {
            ConferenceDisplay();
        }


        #endregion


        #region CALLBACK

        private void TVVS_VideoCalling(string callingID)
        {
            AcceptCall(callingID);
        }

        public void VideoCallResult(CallStatusType e)
        {

            switch (e)
            {
                case CallStatusType.Failed:
                    break;
                case CallStatusType.Checking:
                    break;
                case CallStatusType.None:
                    break;
                case CallStatusType.Offline:
                    break;
                case CallStatusType.Online:
                    break;
                case CallStatusType.Calling:
                    break;
                case CallStatusType.Connected:
                    break;
                case CallStatusType.Busy:
                    break;
                case CallStatusType.DisConnect:
                    DisConnected();
                    break;
                case CallStatusType.NoAnswer:
                    break;
                case CallStatusType.StartCalling:
                    break;
                case CallStatusType.Hangup:
                    Hangup();
                    break;
                default:
                    break;
            }
        }

        #endregion



        #endregion

    }
}
